A h/t to Sharon Machlis for turning people on to the Mapview package, and her excellent walkthroughs of it.
For the data tonight we’ll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let’s quickly review how we use it.
# uncomment to run, then recomment it out so you don't run it every time# census_api_key("APIKEYCODE", install=TRUE)
#chose variables we wantmyvars <-c(totalpop ="B01003_001",medincome ="B19013_001",medage ="B01002_001")
#pull for NY countiesny_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),state ="NY",output ="wide",geometry =TRUE)
Getting data from the 2017-2021 5-year ACS
Warning: • You have not set a Census API key. Users without a key are limited to 500
queries per day and may experience performance limitations.
ℹ For best results, get a Census API key at
http://api.census.gov/data/key_signup.html and then supply the key to the
`census_api_key()` function to use it throughout your tidycensus session.
This warning is displayed once per session.
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Simple feature collection with 62 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -79.76215 ymin: 40.4961 xmax: -71.85621 ymax: 45.01585
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE medincomeM
1 36017 Chenango County, New York 47407 NA 55690 2603
2 36119 Westchester County, New York 999723 NA 105387 1643
3 36025 Delaware County, New York 44644 NA 52757 2293
4 36115 Washington County, New York 61504 NA 63869 2006
5 36075 Oswego County, New York 118019 NA 61983 2218
6 36067 Onondaga County, New York 474621 NA 66012 1000
7 36113 Warren County, New York 65692 NA 68765 3677
8 36051 Livingston County, New York 62253 NA 64467 2308
9 36009 Cattaraugus County, New York 77211 NA 53537 1681
10 36105 Sullivan County, New York 78230 NA 63393 2692
medageE medageM geometry
1 44.7 0.2 MULTIPOLYGON (((-75.88983 4...
2 41.2 0.2 MULTIPOLYGON (((-73.77278 4...
3 48.5 0.3 MULTIPOLYGON (((-75.42264 4...
4 44.4 0.3 MULTIPOLYGON (((-73.63622 4...
5 40.7 0.4 MULTIPOLYGON (((-76.61693 4...
6 39.3 0.2 MULTIPOLYGON (((-76.49931 4...
7 46.8 0.3 MULTIPOLYGON (((-74.21462 4...
8 41.7 0.5 MULTIPOLYGON (((-78.06075 4...
9 42.3 0.4 MULTIPOLYGON (((-79.05908 4...
10 42.3 0.4 MULTIPOLYGON (((-75.14468 4...
#all counties in the USall_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),output ="wide",geometry =TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE
1 20161 Riley County, Kansas 72602 NA 53296
2 19159 Ringgold County, Iowa 4739 NA 57700
3 30009 Carbon County, Montana 10488 NA 63178
4 16007 Bear Lake County, Idaho 6327 NA 60337
5 55011 Buffalo County, Wisconsin 13314 NA 61167
6 31185 York County, Nebraska 14164 NA 66337
7 08037 Eagle County, Colorado 55693 NA 91338
8 42129 Westmoreland County, Pennsylvania 355107 NA 64708
9 40079 Le Flore County, Oklahoma 48436 NA 43049
10 48053 Burnet County, Texas 48424 NA 65363
medincomeM medageE medageM geometry
1 2489 25.5 0.1 MULTIPOLYGON (((-96.96095 3...
2 5058 44.3 1.0 MULTIPOLYGON (((-94.47167 4...
3 4261 50.7 0.9 MULTIPOLYGON (((-109.7987 4...
4 7039 38.9 1.1 MULTIPOLYGON (((-111.6345 4...
5 2352 46.5 0.5 MULTIPOLYGON (((-92.08384 4...
6 4128 39.5 1.2 MULTIPOLYGON (((-97.82629 4...
7 4058 37.8 0.8 MULTIPOLYGON (((-107.1137 3...
8 1350 47.1 0.2 MULTIPOLYGON (((-79.90487 4...
9 1869 38.8 0.3 MULTIPOLYGON (((-95.05996 3...
10 4694 44.7 0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"ny_counties_withgeo <- ny_counties_withgeo %>%select(-ends_with("M"))ny_counties_withgeo
Simple feature collection with 62 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -79.76215 ymin: 40.4961 xmax: -71.85621 ymax: 45.01585
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE medincomeE medageE
1 36017 Chenango County, New York 47407 55690 44.7
2 36119 Westchester County, New York 999723 105387 41.2
3 36025 Delaware County, New York 44644 52757 48.5
4 36115 Washington County, New York 61504 63869 44.4
5 36075 Oswego County, New York 118019 61983 40.7
6 36067 Onondaga County, New York 474621 66012 39.3
7 36113 Warren County, New York 65692 68765 46.8
8 36051 Livingston County, New York 62253 64467 41.7
9 36009 Cattaraugus County, New York 77211 53537 42.3
10 36105 Sullivan County, New York 78230 63393 42.3
geometry
1 MULTIPOLYGON (((-75.88983 4...
2 MULTIPOLYGON (((-73.77278 4...
3 MULTIPOLYGON (((-75.42264 4...
4 MULTIPOLYGON (((-73.63622 4...
5 MULTIPOLYGON (((-76.61693 4...
6 MULTIPOLYGON (((-76.49931 4...
7 MULTIPOLYGON (((-74.21462 4...
8 MULTIPOLYGON (((-78.06075 4...
9 MULTIPOLYGON (((-79.05908 4...
10 MULTIPOLYGON (((-75.14468 4...
#remove that trailing "E"colnames(ny_counties_withgeo) <-sub("E$", "", colnames(ny_counties_withgeo)) # $ means end of string onlyny_counties_withgeo
Simple feature collection with 62 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -79.76215 ymin: 40.4961 xmax: -71.85621 ymax: 45.01585
Geodetic CRS: NAD83
First 10 features:
GEOID NAM totalpop medincome medage
1 36017 Chenango County, New York 47407 55690 44.7
2 36119 Westchester County, New York 999723 105387 41.2
3 36025 Delaware County, New York 44644 52757 48.5
4 36115 Washington County, New York 61504 63869 44.4
5 36075 Oswego County, New York 118019 61983 40.7
6 36067 Onondaga County, New York 474621 66012 39.3
7 36113 Warren County, New York 65692 68765 46.8
8 36051 Livingston County, New York 62253 64467 41.7
9 36009 Cattaraugus County, New York 77211 53537 42.3
10 36105 Sullivan County, New York 78230 63393 42.3
geometry
1 MULTIPOLYGON (((-75.88983 4...
2 MULTIPOLYGON (((-73.77278 4...
3 MULTIPOLYGON (((-75.42264 4...
4 MULTIPOLYGON (((-73.63622 4...
5 MULTIPOLYGON (((-76.61693 4...
6 MULTIPOLYGON (((-76.49931 4...
7 MULTIPOLYGON (((-74.21462 4...
8 MULTIPOLYGON (((-78.06075 4...
9 MULTIPOLYGON (((-79.05908 4...
10 MULTIPOLYGON (((-75.14468 4...
Warning: Found less unique colors (9) than unique zcol values (62)!
Interpolating color vector to match number of zcol values.
Customize popups
mypopup <- glue::glue("<strong>{ny_counties_withgeo$NAM}</strong><br /> Total Population: {ny_counties_withgeo$totalpop}<br /> Median Income: {ny_counties_withgeo$medincome}") %>%lapply(htmltools::HTML)# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%# lapply(htmltools::HTML)
head(mypopup)
[[1]]
<strong>Chenango County, New York</strong><br />
Total Population: 47407<br />
Median Income: 55690
[[2]]
<strong>Westchester County, New York</strong><br />
Total Population: 999723<br />
Median Income: 105387
[[3]]
<strong>Delaware County, New York</strong><br />
Total Population: 44644<br />
Median Income: 52757
[[4]]
<strong>Washington County, New York</strong><br />
Total Population: 61504<br />
Median Income: 63869
[[5]]
<strong>Oswego County, New York</strong><br />
Total Population: 118019<br />
Median Income: 61983
[[6]]
<strong>Onondaga County, New York</strong><br />
Total Population: 474621<br />
Median Income: 66012